package org.ntu.rtsearch.search.model;

import java.util.ArrayList;
import java.util.List;

import org.ntu.rtsearch.index.model.Posting;

/**
 * @author fuhao(email: hfhimage@gmail.com)         
 * @date 2011-7-26
 */
public class Scorer {
	
	private List<Posting> postings = new ArrayList<Posting>();
	
	public int docId = -1;
	
	private long timeStamp; 
	
	public Scorer(long ts) {
		this.timeStamp = ts;
	}
	
	public void addPosting(Posting posting) {
		postings.add(posting);
		if(docId == -1) {
			docId = posting.getDocId();
		}
	}
	
	public double sim() {
		int sum = 0, squareSum = 0;
		int tf;
		for (Posting p : postings) {
			tf = p.getTermFreq();
			sum += tf;
			squareSum += tf * tf;
		}
		return sum / Math.sqrt(squareSum) / Math.sqrt(postings.size());
	}
	
	public double rankScore() {
		double w1 = 1, w2 = 1, C = 1;
		return w1 * sim() + w2 * sig(docId) + C * timeDiff();
	}
	
	public double sig(int docId) {
		return 1;
	}
	
	public double timeDiff() {
		return Math.log1p(timeStamp - postings.get(0).getTimeStamp());
	}
}
